package com.combetter.changxing.viewmodel.base;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Toast;

import com.combetter.changxing.BR;
import com.combetter.changxing.adapter.HomeHeaderItem;
import com.combetter.changxing.bindingconfig.RecyclerViewItemClickSupport;
import com.combetter.changxing.collections.ItemView;
import com.combetter.changxing.collections.ItemViewSelector;
import com.combetter.changxing.collections.LayoutManagers;

import java.util.ArrayList;

/**
 * Created by Mark.fan on 2018/5/14.
 */

public class BaseRecyclerViewModel<T> extends BaseViewModel {
    /**
     * 分页变量
     */
    private int firstPager = 1;

    private int page = firstPager;

    private int pageSize = 6;

    //添加头部布局
    private final ArrayList<HeaderFooterMapping> headers = new ArrayList<>();

    //特殊的item集合，传入对应的 position位置和 layoutId,把原本默认的item layout 转为 指定的 layout，item数据不是插进 items里面额外的数据，而是原本存在的数据。
    private final SparseArray<Integer> specialViews = new SparseArray<>();

    /**
     * Adapter 里的数据内容
     */
    protected final ObservableList<Object> items = new ObservableArrayList<>();

    public LayoutManagers.LayoutManagerFactory mLayoutManager = LayoutManagers.linear();

    private int itemLayout;

    public BaseRecyclerViewModel(@NonNull Application application, int itemLayout) {
        super(application);
        this.itemLayout = itemLayout;
    }

    /**
     * 列表布局管理器
     *
     * @return
     */
    public LayoutManagers.LayoutManagerFactory getLayoutManager() {
        return mLayoutManager;
    }

    private ItemViewSelector<Object> itemViews = new ItemViewSelector<Object>() {
        @Override
        public void select(ItemView itemView, int position, Object item) {
            onItemViewSelector(itemView, position, item);
        }

        @Override
        public int viewTypeCount() {
            return 1;
        }
    };

    public void onItemViewSelector(ItemView itemView, int position, Object item) {
        int layout = itemLayout;
        if (position < headers.size()) {
            layout = headers.get(position).getLayout();
        }
        itemView.setBindingVariable(BR.viewModel)
                .setLayoutRes(layout);
    }

    public void addHeader(int layout, Object o) {
        headers.add(new HeaderFooterMapping(layout, o));
        items.add(headers.size() - 1, o);
    }

    /**
     * 清除列表数据，包括头部或尾部
     */
    public void clear() {
        headers.clear();
        items.clear();
    }

    /**
     * 列表item点击事件
     */
    public RecyclerViewItemClickSupport.OnItemClickListener onItemClickListener = new RecyclerViewItemClickSupport.OnItemClickListener() {
        @Override
        public void onItemClick(RecyclerView recyclerView, int position, View view) {
            try {
                //refresh之后会clean items的数据，这个瞬间有可能造成点击 超出数组范围异常
                BaseRecyclerViewModel.this.onItemClick(view, position, items.get(position));
            } catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    };

    public RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            int totalItemCount = layoutManager.getItemCount();
            if (lastVisibleItem >= totalItemCount - 1 && dy > 0) {
                onLoadMore();
            }
        }
    };

    /**
     * 在viewModel中回调此方法进行逻辑处理
     *
     * @param view
     * @param position
     * @param item
     */
    public void onItemClick(View view, int position, Object item) {

    }

    /**
     * 加载更多
     */
    public void onLoadMore() {
    }


    public RecyclerViewItemClickSupport.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    /**
     * 设置特殊的样式
     *
     * @param position item的位置
     * @param layout   改为对应的layout
     */
    public void setSpecialView(int position, int layout) {
        specialViews.append(position, layout);
    }

    /**
     * 清除所有特殊样式
     */
    public void clearSpecialViews() {
        specialViews.clear();
    }

    /**
     * 清除指定位置的样式
     *
     * @param position 位置
     */
    public void removeSpecialView(int position) {
        specialViews.remove(position);
    }


    public ObservableList<Object> getItems() {
        return items;
    }

    public ItemViewSelector<Object> getItemViews() {
        return itemViews;
    }

//    public void
// (LayoutManagers.LayoutManagerFactory layoutManager) {
//        mLayoutManager = layoutManager;
//    }

    /**
     * 轮播图点击事件
     */
    public void onBannerClick(int position) {
        BaseRecyclerViewModel.this.onBannerClick(position);
        HomeHeaderItem item = (HomeHeaderItem) headers.get(0).getObject();
        Toast.makeText(getApplication(), "baner is click" + item.getTopStories().get(position).getTitle(), Toast.LENGTH_SHORT).show();
    }


}
